<script lang="ts" setup>
import { useGlobalStore } from '@/stores/modules/global'
import { useThemeStore } from '@/stores/modules/theme'
import { useMenuStore } from '@/stores/modules/menu'

const route = useRoute()
const router = useRouter()
const globalStore = useGlobalStore()
const themeStore = useThemeStore()
const menuStore = useMenuStore()

const activeMenu = ref(route.path)

const themeConfig = computed(() => themeStore.themeConfig)
const menuList = computed(() => globalStore.showMenuList())
// const activeMenu = computed(
//   () => (route.meta.activeMenu ? route.meta.activeMenu : route.path) as string
// )

watch(
  () => route.path,
  (val) => {
    const menuItem = menuStore.menuMap.get(val)
    if (menuItem && menuItem.activeMenu) activeMenu.value = menuItem.activeMenu
    else activeMenu.value = val
  }
)

const logout = () => {
  globalStore.clearToken()
  router.replace('/login')
}

const onMenuSelect = (key: string) => {
  const _oldGroup = activeMenu.value + '=' + key
  setTimeout(() => {
    if (_oldGroup === activeMenu.value + '=' + key) {
      activeMenu.value = key
      setTimeout(() => {
        activeMenu.value = _oldGroup.split('=')[0]
      })
    }
  }, 0)
}
</script>

<template>
  <el-container>
    <el-header>
      <div class="f-c">
        <img
          class="w-50 h-50 object-contain p-11 mr-10"
          src="@/assets/images/logo.png"
          alt="logo"
        />
        <span class="font-size-18 font-bold color-black-primary">{{ themeConfig.name }}</span>
      </div>
      <div class="flex-1 f-e">
        <SizeIcon />
        <MessageIcon />
        <el-dropdown trigger="click">
          <img
            class="w-40 h-40 rounded-full m-r-20 b-1-#e6e6e6 cursor-pointer"
            src="@/assets/logo.svg"
          />
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>个人信息</el-dropdown-item>
              <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>
    <el-container style="height: calc(100vh - 60px)">
      <el-aside
        :style="{
          width: themeConfig.isCollapse
            ? '65px'
            : themeConfig.componentSize === 'small'
              ? '190px'
              : '210px',
          paddingBottom: 'var(--layout-tabs-height)'
        }"
      >
        <CollapseIcon />
        <el-scrollbar>
          <el-menu
            :router="false"
            :default-active="activeMenu"
            :collapse="themeConfig.isCollapse"
            unique-opened
            :collapse-transition="false"
            @select="onMenuSelect"
          >
            <SubMenu :menu-list="menuList" />
          </el-menu>
        </el-scrollbar>
      </el-aside>
      <el-container>
        <Main />
      </el-container>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.el-header {
  height: 60px;
  padding: 0;
  background: #fff;
  border-bottom: 1px solid #f3f3f4;
  display: flex;
}
.el-aside {
  width: auto;
  position: relative;
  background: #fff;
  overflow: visible;
  border-right: 1px solid #f3f3f4;
  transition: width 0.3s ease;
}
</style>
